package cn.gorit.admin.service;

import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.lang.Result;
import cn.gorit.entity.user.User;
import cn.gorit.mapper.user.UserMapper;
import cn.gorit.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version v1.0
 * @Classname UserService
 * @Description TODO
 * @Author CodingGorit
 * @Created on 2021/4/16 0:21
 * @IDLE IntelliJ IDEA
 **/
@Service
public class UserService {

    @Autowired
    private UserMapper mapper;

    // 后台分页获取用户列表参数, 下次开发记得增加 UserDto。后台只展示必要的数据，做一个简单地额 map 映射
    public Page<User> getByPage(Page<User> page) {
        // 查询数据
        List<User> useList = mapper.getByPage(page);
        page.setList(useList);
        // 查询总数
        int totalCount = mapper.getCountByPage(page);
        page.setTotalCount(totalCount);
        return page;
    }

    // 冻结用户
    public Result<Object> freezeUser(String id, Integer deleted) {
        // deleted = -1 表示账号冻结，-2 表示注销
        Integer res = mapper.freezeUser(id,deleted);
        if (res > 0 && deleted.equals(-1))
            return new Result<>(ResultEnums.FREEZE_SUCCESS.getCode(),ResultEnums.FREEZE_SUCCESS.getMsg());
        if (res > 0 && deleted.equals(0))
            return new Result<>(ResultEnums.FREEZE_SUCCESS.getCode(),"取消冻结");
        return new Result<>(ResultEnums.FREEZE_FAILED.getCode(),ResultEnums.FREEZE_FAILED.getMsg());
    }
}
